<?php
/**
 *    记录用户提交的URL，并保存到数据库
 * 
 *    哦，这个是在HOOK在调用的。。。跟你们说一下。
 * 
 */

class logs{
	
	function __construct(){
		 $this->CI = &get_instance();
	}
	
	
	function logurl()
	{
		if(!empty($_SERVER['PHP_SELF'])&&!empty($_SERVER['SERVER_NAME'])&&!empty($_SERVER['SERVER_PORT'])){
			
			if(!empty($_SERVER['QUERY_STRING'])){
			  $url = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
			}
			else 
			{
			  $url = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/'.$_SERVER['PHP_SELF'];
			}
			
		}
		$ip = $this->getClientIp();
		$post = $this->getPOST();
		if($post == false){
			 $post = "none";
		}
		
		$data = array(
		            'ip'=>$ip,
		            'method'=> $_SERVER['REQUEST_METHOD'],
		            'url'=>$url,
		            'post'=>$post,
		            'time'=>time()
		
		            );
		$this->CI->db->insert('logs',$data);
		
	}
	/**
	 * 
	 * 
	 * Get client ip address ...
	 */
	function getClientIp()
	{
		if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
			 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}elseif(!empty($_SERVER['REMOTE_ADDR'])){
			 $ip = $_SERVER['REMOTE_ADDR'];
		}
		
		if(!isset($ip)){
			$ip = 'none';
			return $ip;
		}else{
			return $ip;
		}
	}
	
	/**
	 * 
	 * 
	 *  Get $_POST data,if emtpy return false..
	 */
	function getPOST()
	{
		if(!empty($_POST)){
		    $data = '';
		    foreach($_POST as $key=>$val)
		    {
		    	$data.=$key.'=';
				if(is_array($val)){
		    	$data.=json_encode($val).'&';
				}else{
				$data.=$val.'&';
				}
		    }
		}else{
			return false;
		}
		return $data;
	}
}